package com.SFA.Web;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebSettings.RenderPriority;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.FrameWork.Common.AbstractBaseActivity;
import com.FrameWork.Common.QueryStringHelper;
import com.SFA.R;

public class WebViewActivity extends AbstractBaseActivity
{
	private WebView      webView;
	private Handler      handler;
	private Intent       intent;
	public String	URL	    = null;
	public TextView	tvTitle	    = null;
	private Button		btnReturn;
	private RelativeLayout layoutToolBar= null;
	private LinearLayout layoutProgress = null;
	private AlertDialog  alertDialog;

	/***
	 * 初始化webview
	 */
	private void InItWebView()
	{
		webView = (WebView) findViewById(R.id.webview);
		webView.setHorizontalScrollBarEnabled(false);
		webView.setVerticalScrollBarEnabled(false);
		webView.getSettings().setJavaScriptEnabled(true);
		webView.getSettings().setSaveFormData(false);
		webView.getSettings().setRenderPriority(RenderPriority.HIGH);
		webView.getSettings().setBlockNetworkImage(true);
		webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);  
		webView.setWebChromeClient(new WebChromeClient()
		{

			@Override
			public void onProgressChanged(WebView view, int newProgress)
			{
				if (newProgress == 100)
				{
					layoutProgress.setVisibility(View.GONE);
					webView.setVisibility(View.VISIBLE);
					return;
				}
				super.onProgressChanged(view, newProgress);
			}

		});

		webView.setWebViewClient(new WebViewClient()
		{
			

			@Override
			public void onPageFinished(WebView view, String url)
			{
				view.getSettings().setBlockNetworkImage(false);
				super.onPageFinished(view, url);
			}

			/*@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler,
					SslError error)
			{
				handler.proceed();
			}*/

		});

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK
				&& event.getRepeatCount() == 0)
		{
			finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.webview);
		tvTitle = (TextView) findViewById(R.id.tvTitle);
		layoutToolBar = (RelativeLayout) findViewById(R.id.layoutToolBar);
		btnReturn = (Button) findViewById(R.id.btnReturn);
		// 返回
		btnReturn.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		intent = getIntent();
		Boolean ShowToolBar = intent.getBooleanExtra("ShowToolBar", false);
		if(!ShowToolBar)
		{
			this.layoutToolBar.setVisibility(View.GONE);
		}
		URL = intent.getStringExtra("URL");
		tvTitle.setText(intent.getStringExtra("title"));
		createhandler();
		InItWebView();
		layoutProgress = (LinearLayout) findViewById(R.id.layoutProgress);
		layoutProgress.setVisibility(View.VISIBLE);
		webView.setVisibility(View.GONE);

		//对URL进行加密处理
		if(URL.indexOf("?") ==-1)
			URL= URL+"?" + QueryStringHelper.BuildQueryString(this, new String[]{"UID"}, new String[]{"30"});
		if (URL != null && !URL.equals(""))
			workThread();
		

	}

	private Thread thread = null;

	/***
	 * workThread
	 */
	public void workThread()
	{
		thread = new Thread(new Runnable()
		{
			@Override
			public void run()
			{
				try
				{
					webView.loadUrl(URL);
					
				}
				catch (Exception err)
				{
					handler.sendEmptyMessage(0);
				}

			}
		});
		thread.start();
	}

	/***
	 * creathandler
	 */

	public void createhandler()
	{
		handler = new Handler()
		{
			@Override
			public void handleMessage(Message msg)
			{

				alertDialog = new AlertDialog.Builder(WebViewActivity.this).setTitle("网络异常请查看网络连接是否正常")
				.setIcon(android.R.drawable.ic_dialog_alert)
				.setPositiveButton(" 确  定 ",
						new DialogInterface.OnClickListener()
				{
					@Override
					public void onClick(DialogInterface dialog,
							int which)
					{
						dialog.cancel();
						finish();
					}
				})
				.create();

				// 屏蔽dialog显示时的返回键
				alertDialog.setCancelable(false);
				alertDialog.show();
				super.handleMessage(msg);
			}
		};

	}
}
